package com.sxpi.zixun.service.resourcecategory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.ResourceCategoryBo;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.ResourceCategoryMapper;
import com.sxpi.zixun.pojo.ResourceCategory;
import com.sxpi.zixun.service.category.ICategoryService;
import com.sxpi.zixun.vo.ResourceCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/2 19:24
 */
@Service
@Slf4j
public class IResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMapper, ResourceCategory> implements IResourceCategoryService {

    /**
     * 查询资源分类列表信息
     * @return
     */
    @Override
    public List<ResourceCategoryBo> findResourceCategoryList() {
        // 1：设置查询条件
        LambdaQueryWrapper<ResourceCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2：查询发布的 0 未发布  1 发布
        lambdaQueryWrapper.eq(ResourceCategory::getStatus,1);
        lambdaQueryWrapper.eq(ResourceCategory::getIsdelete,0);
        // 3: 查询返回
        return tranferListBo(this.list(lambdaQueryWrapper),ResourceCategoryBo.class);
    }

    /**
     * 查询资源分类列表信息并分页
     * @param resourcecategoryVo
     * @return
     */
    @Override
    public IPage<ResourceCategoryBo> findResourceCategoryPage(ResourceCategoryVo resourcecategoryVo) {
        // 设置分页信息
        Page<ResourceCategory> page = new Page<>(resourcecategoryVo.getPageNo(),resourcecategoryVo.getPageSize());
        // 设置查询条件
        LambdaQueryWrapper<ResourceCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // lambdaQueryWrapper.select(ResourceCategory.class, column -> !column.getColumn().equals("description"));
        // 根据关键词搜索信息
        lambdaQueryWrapper.like(Vsserts.isNotEmpty(resourcecategoryVo.getKeyword()), ResourceCategory::getTitle,resourcecategoryVo.getKeyword());
        //查询发布的 0 未发布  1 发布
        lambdaQueryWrapper.eq(resourcecategoryVo.getStatus() != null ,ResourceCategory::getStatus,resourcecategoryVo.getStatus());
        lambdaQueryWrapper.eq(ResourceCategory::getIsdelete,0);
        // 多列搜索
        // lambdaQueryWrapper.and(Vsserts.isNotEmpty(resourcecategoryVo.getKeyword()),wrapper -> wrapper
        //         .like(ResourceCategory::getName, resourcecategoryVo.getKeyword())
        //         .or()
        //         .like(ResourceCategory::getCategoryName, resourcecategoryVo.getKeyword())
        // );
        // 根据时间排降序
        lambdaQueryWrapper.orderByDesc(ResourceCategory::getCreateTime);
        // 查询分页返回
        IPage<ResourceCategory> results = this.page(page,lambdaQueryWrapper);
        return tranferPageBo(results,ResourceCategoryBo.class);
    }

    /**
     * 根据资源分类id查询明细信息
     * @param id
     * @return
     */
    @Override
    public ResourceCategoryBo getResourceCategoryById(Long id) {
        return tranferBo(this.getById(id),ResourceCategoryBo.class);
    }

    /**
     * 保存和修改资源分类
     * @param resourcecategory
     * @return
     */
    @Override
    public ResourceCategoryBo saveUpdateResourceCategory(ResourceCategory resourcecategory) {
        boolean flag = this.saveOrUpdate(resourcecategory);
        return flag ? tranferBo(resourcecategory,ResourceCategoryBo.class)  : null;
    }

    /**
     * 根据资源分类id删除资源分类
     * @param id
     * @return
     */
    @Override
    public int deleteResourceCategoryById(Long id) {
        ResourceCategory adminRole = this.getById(id);
        if (adminRole != null) {
            ResourceCategory resourceCategoryNew = new ResourceCategory();
            resourceCategoryNew.setId(id);
            resourceCategoryNew.setIsdelete(1);
            boolean b = this.updateById(resourceCategoryNew);
            return 1;
        }
        return 0;
    }
}
